﻿using System;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.CRM.AppCode.Lib;

namespace mojoPortal.Web.RS.CRM.Controls
{
    public partial class ModalCreateClientControl : RSBaseUserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void ShowModal(int companyid, int EmployeeID)
        {
            hfIsDirty.Value = "";
            //RSUserIdentity userIdentity = GetRSUserIdentity();
            //txtAccountOwner.Text = userIdentity.RSFirstName + " " + userIdentity.RSLastName;
            mpeModalCreateClient.Show();
        }

        protected void btnModalOK_Click(Object sender, EventArgs e)
        {
            mpeModalCreateClient.Hide();
            if (hfIsDirty.Value == "1")
            {
                Response.Redirect(Request.RawUrl);
            }
        }

        protected void btnSaveClient_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int CompanyId = userIdentity.RSCompanyId;

            string SQL = "INSERT INTO rs_tblClient " +
                        "(AccountOwner, " +
                        "InsuredAnnuitantName, " +
                        "AccountNumber, " +
                        "AccountType, " +
                        "FirstName, " +
                        "MiddleName, " +
                        "LastName, " +
                        "Title, " +
                        "Email, " +
                        "Phone, " +
                        "Mobile, " +
                        "Fax, Audit_CreateBy, Audit_CreateDt, AgentId, CompanyId) " +
                        "VALUES " +
                        "(@AccountOwner, " +
                        "@InsuredAnnuitantName, " +
                        "@AccountNumber, " +
                        "@AccountType, " +
                        "@FirstName, " +
                        "@MiddleName, " +
                        "@LastName, " +
                        "@Title, " +
                        "@Email, " +
                        "@Phone, " +
                        "@Mobile, " +
                        "@Fax, @CurrentUser, GETDATE(), @AgentId, @CompanyId)";

            string AccountOwner = txtAccountOwner.Text.Trim();
            string InsuredAnnuitantName = txtInsuredAnnuitantName.Text.Trim();
            string AccountNumber = txtAccountNumber.Text.Trim();
            string AccountType = txtAccountType.Text.Trim();
            string FirstName = txtFirstName.Text.Trim();
            string MiddleName = txtMiddleName.Text.Trim();
            string LastName = txtLastName.Text.Trim();
            string Title = txtTitle.Text.Trim();
            string Email = txtEmail.Text.Trim();
            string Phone = txtPhone.Text.Trim();
            string Mobile = txtMobile.Text.Trim();
            string Fax = txtFax.Text.Trim();
            int AgentId = userIdentity.RSEmployeeId;

            SqlCommand sqlCmd = new SqlCommand();

            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@AccountOwner", SqlDbType.NVarChar, 50, ParameterDirection.Input, AccountOwner);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@InsuredAnnuitantName", SqlDbType.NVarChar, 50, ParameterDirection.Input, InsuredAnnuitantName);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@AccountType", SqlDbType.NVarChar, 20, ParameterDirection.Input, AccountType);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@AccountNumber", SqlDbType.NVarChar, 15, ParameterDirection.Input, AccountNumber);

            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Fax", SqlDbType.NVarChar, 15, ParameterDirection.Input, Fax);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Phone", SqlDbType.NVarChar, 15, ParameterDirection.Input, Phone);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Email", SqlDbType.NVarChar, 50, ParameterDirection.Input, Email);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Mobile", SqlDbType.NVarChar, 15, ParameterDirection.Input, Mobile);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Title", SqlDbType.NVarChar, 25, ParameterDirection.Input, Title);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@FirstName", SqlDbType.NVarChar, 25, ParameterDirection.Input, FirstName);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@MiddleName", SqlDbType.NVarChar, 25, ParameterDirection.Input, MiddleName);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@LastName", SqlDbType.NVarChar, 25, ParameterDirection.Input, LastName);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 25, ParameterDirection.Input, userIdentity.RSCurrentUser);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@AgentId", SqlDbType.Int, 0, ParameterDirection.Input, AgentId);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@CompanyId", SqlDbType.Int, 0, ParameterDirection.Input, CompanyId);
            SQLHelper.SetCommandType(sqlCmd, CommandType.Text, SQL);
            SQLHelper.ExecuteScalarCmd(sqlCmd);

            int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
            lblCreateClientHeader.Text = "Client Successfully Created!";
            lblCreateClientHeader.ForeColor = System.Drawing.Color.DarkBlue;
            btnSaveClient.Enabled = false;
            hfIsDirty.Value = "1";
        }

    }
}